package aplicacao.repositorios;

import java.sql.*;

public class ConectorMySql {

	private static final String url = "jdbc:mysql://localhost:3306/academinha";
	private static final String login = "root";
	private static final String senha = "";
	private Connection conn;
	private static ConectorMySql instancia = null;
	
	private ConectorMySql() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			System.out.println("\nDriver carregado com sucesso!\n");
			try {
				conn = DriverManager.getConnection(url, login, senha);

			} catch (Exception ex) {
				System.out.println("\nErro na connection!");
			}
		} catch (Exception ex) {
			System.out.println("\nDriver nao pode ser carregado!");
		}
	}

	public static ConectorMySql getInstance(){
		if(instancia == null){
			instancia = new ConectorMySql();
		}
		
		return instancia;
	}
	
	public int executar(String comando) {
		int rs = 0;
		try {
			String sql = comando;
			Statement stm = conn.createStatement();
			try {
				rs = stm.executeUpdate(sql);
				//System.out.println("\nSucesso!!!\n");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} catch (Exception ex) {
			System.out.println("\nErro no statement!");
		}

		return rs;
	}
	
	public ResultSet query(String comando) {
		ResultSet rs = null;
		try {
			String sql = comando;
			Statement stm = conn.createStatement();
			try {
				rs = stm.executeQuery(sql);
				//System.out.println("\nSucesso!!!\n");
			} catch (Exception ex) {
				System.out.println("\nErro no resultset!");
			}
		} catch (Exception ex) {
			System.out.println("\nErro no statement!");
		}

		return rs;
	}
}
